<!DOCTYPE html>
<html lang="en">
    <head>
  <!-- 元数据 -->
  <meta charset="utf-8">
  <link rel="icon" href="https://gitee.com/cuishikang/tmp/raw/master/img/boke.png">
  <title>Vue2 install插件案例 | 小崔的博客</title>
  <meta name="author" content="cuishikang" />
  <meta http-equiv="Cache-Control" content="no-transform" />
  <meta http-equiv="Cache-Control" content="no-siteapp" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="robots" content="index,follow" />
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
  <meta name="format-detection" content="telphone=no, email=no" />
  
    <meta name="keywords" content="Vue" />
  
  <meta name="description" content="::: tip 介绍Vue2插件 绝对时间插件 和 图片失效 制作方法 :::">
<meta property="og:type" content="article">
<meta property="og:title" content="Vue2 install插件案例">
<meta property="og:url" content="https://cuishikang.gitee.io/2021/06/30/Vue/3_Vue_install/index.html">
<meta property="og:site_name" content="小崔的博客">
<meta property="og:description" content="::: tip 介绍Vue2插件 绝对时间插件 和 图片失效 制作方法 :::">
<meta property="og:locale" content="en_US">
<meta property="og:image" content="https://gitee.com/cuishikang/tmp/raw/master/img/boke.png">
<meta property="article:published_time" content="2021-06-29T16:00:00.000Z">
<meta property="article:modified_time" content="2022-02-19T10:22:58.542Z">
<meta property="article:author" content="cuishikang">
<meta property="article:tag" content="Vue">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://gitee.com/cuishikang/tmp/raw/master/img/boke.png">
<meta name="twitter:site" content="@null">
  
  <!-- 站点验证相关 -->
  
    
    
    
  
  <!-- 样式表文件 -->
  <link rel="stylesheet" id="kratos-css" href="/css/kratosr.min.css" type="text/css" media="all">
  
    <link rel="stylesheet" id="highlight-css" href="/css/highlight/night-eighties.min.css" type="text/css" media="all">
  
  
  <link rel="stylesheet" id="fontawe-css" href="https://unpkg.com/font-awesome@4.7.0/css/font-awesome.min.css" type="text/css" media="all">
  <link rel="stylesheet" id="nprogress-css" href="https://unpkg.com/nprogress@0.2.0/nprogress.css" type="text/css" media="all">
  
  
    <link rel="stylesheet" href="https://unpkg.com/aplayer@1.10.1/dist/APlayer.min.css">
  
  
    <link rel="stylesheet" href="https://unpkg.com/@fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.css">
  
  
    <link rel="stylesheet" id="darkmode-css" href="/css/kr-dark.min.css" type="text/css" media="all">
  
  <!-- 不得不预先加载的一些JS文件 -->
  <script src="https://unpkg.com/jquery@3.6.0/dist/jquery.min.js"></script>
  
    <script src="https://unpkg.com/qrcode_js@1.0.0/qrcode.min.js"></script>
  
  
  <style>
    
      .kratos-cover.kratos-cover-2 {
        background-image: url('/images/banner.webp');
      }
    
    
      @media(min-width:768px) {
        body.custom-background {
          background-image: url('/images/bg.webp');
        }
      }
    
  </style>
  
<meta name="generator" content="Hexo 6.0.0"></head>


    <body class="custom-background">
        <div id="kratos-wrapper">
    <div id="kratos-page">
        <div id="kratos-header">
            <header id="kratos-desktop-topnav" class="kratos-topnav">
                <div class="container">
                    <div class="nav-header">
                        <nav id="kratos-menu-wrap">
                            <ul id="kratos-primary-menu" class="sf-menu">
                                
                                    
                                        <li><a href="/"><i class="fa fa-home"></i>首页</a></li>
                                    
                                
                                    
                                        <li><a href="/archives/"><i class="fa fa-file"></i>档案馆</a></li>
                                    
                                
                                    
                                        <li><a href="/friends/"><i class="fa fa-paw"></i>好伙伴</a></li>
                                    
                                
                                    
                                        <li>
                                            <a><i class="fa fa-link"></i>关于本人</a>
                                            <ul class="sub-menu">
                                                
                                                    
                                                
                                                    
                                                        <li><a target="_blank" rel="noopener" href="https://gitee.com/cuishikang"><i class="fa fa-git"></i>码云</a></li>
                                                    
                                                
                                                    
                                                        <li><a target="_blank" rel="noopener" href="https://gitee.com/cuishikang/tmp/raw/master/img/2201645358312_.pic.jpg"><i class="fa fa-weixin"></i>微信</a></li>
                                                    
                                                
                                                    
                                                        <li><a target="_blank" rel="noopener" href="https://www.zhihu.com/people/fu-xi-ting-67-17-5"><i class="fa  fa-search"></i>知乎</a></li>
                                                    
                                                
                                                    
                                                        <li><a href="http://cuishikang.gitee.io/my_resume/#/"><i class="fa  fa-file-o"></i>简历</a></li>
                                                    
                                                
                                            </ul>
                                        </li>
                                    
                                
                            </ul>
                        </nav>
                    </div>
                </div>
            </header>
            <header id="kratos-mobile-topnav" class="kratos-topnav">
                <div class="container">
                    <div class="color-logo"><a href="/">小崔的博客</a></div>
                    <div class="nav-toggle">
                        <a class="kratos-nav-toggle js-kratos-nav-toggle">
                            <i></i>
                        </a>
                    </div>
                </div>
            </header>
        </div>
        <div class="kratos-start kratos-hero-2">
            <!-- <div class="kratos-overlay"></div> -->
            <div class="kratos-cover kratos-cover-2 text-center">
                <div class="desc desc2 animate-box">
                    <a href="/">
                        <h2>小崔的博客</h2> <br />
                        <span></span>
                    </a>
                </div>
            </div>
        </div>

        <div id="kratos-blog-post">
            <div class="container">
                <div id="main" class="row">
                    

        

            <section class="col-md-8">

        

            <article>
    <div class="kratos-hentry kratos-post-inner clearfix">
        <header class="kratos-entry-header">
            
                <h1 class="kratos-entry-title text-center">Vue2 install插件案例</h1>
            
            
            <ul class="kratos-post-meta text-center">
                <li><i class="fa fa-calendar"></i> 2021-06-30</li>
                <li><i class="fa fa-user"></i> 作者 cuishikang</li>
                <li>
                    <i class="fa fa-edit"></i> 
                    
                    
                        ~4.60K
                    
                    字
                </li>
                
            </ul>
        </header>
        <div class="kratos-post-content">
            <div id="expire-alert" class="alert alert-warning hidden" role="alert">
                本文最后编辑于 <time datetime="1645266178542"></time> 前，其中的内容可能需要更新。
            </div>
            
            <hr />
            <p>::: tip 介绍<br>Vue2插件 绝对时间插件 和 图片失效 制作方法 <br><br>:::</p>
<span id="more"></span>

<h2 id="Vue2的插件机制-install"><a href="#Vue2的插件机制-install" class="headerlink" title="Vue2的插件机制 install"></a>Vue2的插件机制 <code>install</code></h2><p><a target="_blank" rel="noopener" href="https://v3.cn.vuejs.org/guide/plugins.html#%E6%8F%92%E4%BB%B6">Vue插件介绍</a></p>
<blockquote>
<p>目标：熟悉Vue的插件机制（针对Vue核心规则的一种扩展机制）</p>
</blockquote>
<p>只要用<code>Vue.use()</code>导入的 都是Vue插件扩展</p>
<ul>
<li><font color = #ff3040>Vue.use(参数1,参数2)</font><ul>
<li>参数1 是需要实例化的插件名称</li>
<li>参数2 是定制插件内部的信息(插件接收的数据 传递给install的options参数 可设置为固定值)</li>
</ul>
</li>
</ul>
<blockquote>
<p>第一步 定义插件（Vue插件就是一个对象，对象中必须有一个<code>install</code>方法）</p>
</blockquote>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// Vue插件就是一个对象，对象中必须有一个install方法</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">  install (<span class="title class_">Vue</span>,options) &#123;</span><br><span class="line">    <span class="comment">// 该方法的形参就是Vue构造函数 </span></span><br><span class="line">    <span class="comment">// 给Vue的实例化对象里面 添加$abc方法</span></span><br><span class="line">    <span class="title class_">Vue</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">$abc</span> = <span class="number">123</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<blockquote>
<p>第二步 导入并配置插件(在总组件导入文件设置)</p>
</blockquote>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 导入自定义插件</span></span><br><span class="line"><span class="keyword">import</span> <span class="title class_">MyPlugins</span> <span class="keyword">from</span> <span class="string">&#x27;@/utils/plugins.js&#x27;</span></span><br><span class="line"><span class="comment">// 导入完毕后 需要Vue.use() 把组件添加到Vue实例化对象里</span></span><br><span class="line"><span class="title class_">Vue</span>.<span class="title function_">use</span>(<span class="title class_">MyPlugins</span>,<span class="string">&#x27;这里可以导入一些数据&#x27;</span>) <span class="comment">// 配置和定义插件时候 支持配置选项</span></span><br></pre></td></tr></table></figure>

<blockquote>
<p>第三步 在需要调用的Vue组件里 使用</p>
</blockquote>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 组件中，可以用如下方式访问实例属性 </span></span><br><span class="line"><span class="comment">// 直接调用导入的Vue实例化方法即可 $abc</span></span><br><span class="line"><span class="variable language_">this</span>.<span class="property">$abc</span></span><br></pre></td></tr></table></figure>

<h2 id="绝对时间插件制作例-基于dayjs"><a href="#绝对时间插件制作例-基于dayjs" class="headerlink" title="绝对时间插件制作例 基于dayjs"></a>绝对时间插件制作例 <code>基于dayjs</code></h2><p><img src="https://tva4.sinaimg.cn/large/005INI3Xly8gs44pcc3kuj30bo023748.jpg" alt="Snipaste_2021-07-03_22-22-49"></p>
<p><a target="_blank" rel="noopener" href="https://dayjs.fenxianglu.cn/category/plugin.html#%E7%9B%B8%E5%AF%B9%E6%97%B6%E9%97%B4">dayjs的相对时间官网</a></p>
<p><a target="_blank" rel="noopener" href="https://v3.cn.vuejs.org/guide/plugins.html#%E6%8F%92%E4%BB%B6">Vue插件介绍官网</a></p>
<blockquote>
<p>目标：自定义过滤器格式化时间 (相当于时间)</p>
<p>安装: npm install dayjs –save</p>
</blockquote>
<p>通过 dayjs提供的包 制作时间过滤器插件</p>
<ul>
<li><code>Vue.use()</code>实例化 创建一个过滤器插件</li>
</ul>
<blockquote>
<p>在工具文件夹 <code>utils文件夹 里面 plugins.js</code> 创建一个时间过滤器插件<code>filter</code></p>
</blockquote>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 自定义Vue插件的文件</span></span><br><span class="line"><span class="comment">// Vue插件就是一个对象 对象中必须包含一个install方法</span></span><br><span class="line"><span class="comment">// 导入时间过滤器的插件</span></span><br><span class="line"><span class="keyword">import</span> dayjs <span class="keyword">from</span> <span class="string">&#x27;dayjs&#x27;</span></span><br><span class="line"><span class="comment">// 导入时间过滤器的 相对时间模板</span></span><br><span class="line"><span class="keyword">import</span> relativeTime <span class="keyword">from</span> <span class="string">&#x27;dayjs/plugin/relativeTime&#x27;</span></span><br><span class="line"><span class="comment">// 导入国际化(中文)</span></span><br><span class="line"><span class="keyword">import</span> <span class="string">&#x27;dayjs/locale/zh-cn&#x27;</span></span><br><span class="line"><span class="comment">// 把 相对时间 和 时间过滤器插件 进行关联</span></span><br><span class="line">dayjs.<span class="title function_">extend</span>(relativeTime)</span><br><span class="line"><span class="comment">// 设置一个时间过滤器</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">  install (<span class="title class_">Vue</span>) &#123;</span><br><span class="line">    <span class="comment">// 扩展日期格式化过滤器 添加一个filter过滤器 formatTime是过滤名称</span></span><br><span class="line">    <span class="title class_">Vue</span>.<span class="title function_">filter</span>(<span class="string">&#x27;formatTime&#x27;</span>, <span class="keyword">function</span> (<span class="params">time</span>) &#123;</span><br><span class="line">      <span class="comment">// 基于中文的方式计算time的相对时间</span></span><br><span class="line">      <span class="keyword">return</span> <span class="title function_">dayjs</span>().<span class="title function_">locale</span>(<span class="string">&#x27;zh-cn&#x27;</span>).<span class="title function_">from</span>(time) <span class="comment">// dayjs方法.中文显示.格式化时间</span></span><br><span class="line">    &#125;)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<blockquote>
<p>在项目入口文件 导入制作的时间插件 <code>main.js</code></p>
</blockquote>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//~ 导入时间过滤器功能</span></span><br><span class="line"><span class="keyword">import</span> time <span class="keyword">from</span> <span class="string">&#x27;./utils/plugins&#x27;</span></span><br><span class="line"><span class="comment">// ~ 导入 自定义事件过滤器</span></span><br><span class="line"><span class="title class_">Vue</span>.<span class="title function_">use</span>(time)</span><br></pre></td></tr></table></figure>

<blockquote>
<p>在需要的Vue文件 使用过滤器 过滤器使用<code>|</code> <code>main.vue</code></p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&lt;span&gt;&#123;&#123;item.pubdate|formatTime&#125;&#125;&lt;/span&gt;</span><br></pre></td></tr></table></figure>

<h2 id="普通格式化时间制作例-基于-moment"><a href="#普通格式化时间制作例-基于-moment" class="headerlink" title="普通格式化时间制作例 基于 moment"></a>普通格式化时间制作例 <code>基于 moment</code></h2><p><img src="https://tva4.sinaimg.cn/large/005INI3Xly8gs44xqw7t8j304b05rt8l.jpg" alt="image-20210703223417137"></p>
<p><a target="_blank" rel="noopener" href="http://momentjs.cn/">moment官方网站</a></p>
<blockquote>
<p>安装: npm install moment <em>–save</em>  </p>
<p>目标: 设置全局组件 绑定在全局过滤器上 实现绑定的时间格式化</p>
</blockquote>
<p>通过 moment提供的第三方包实现</p>
<ul>
<li>通过<code>Vue.filter()</code> 方法全局绑定过滤器</li>
</ul>
<blockquote>
<p><strong>在工具文件夹</strong> <strong><code>utils文件夹 里面 plugins.js</code></strong> 创建一个时间过滤器插件<code>filter</code></p>
</blockquote>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 导入 moment 时间过滤器第三方包</span></span><br><span class="line"><span class="keyword">import</span> moment <span class="keyword">from</span> <span class="string">&#x27;moment&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 定义一个Vue插件并且导出</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">    <span class="comment">//! 设置全局过滤器 格式化时间 Vue.filter()是绑定全局过滤器</span></span><br><span class="line">    <span class="title class_">Vue</span>.<span class="title function_">filter</span>(<span class="string">&#x27;formatTime&#x27;</span>, <span class="function"><span class="params">value</span> =&gt;</span> &#123;</span><br><span class="line">      <span class="comment">// 通过第三方包 moment 设置格式化时间 调用时候 |formatTime</span></span><br><span class="line">      <span class="comment">// yyyy-MM-DD 时间格式是 年-月-日 </span></span><br><span class="line">      <span class="keyword">return</span> <span class="title function_">moment</span>(value).<span class="title function_">format</span>(<span class="string">&#x27;yyyy-MM-DD&#x27;</span>) </span><br><span class="line">    &#125;)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<blockquote>
<p>在项目入口文件 导入制作的时间插件 <code>main.js</code></p>
</blockquote>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//~ 导入时间过滤器功能</span></span><br><span class="line"><span class="keyword">import</span> time <span class="keyword">from</span> <span class="string">&#x27;./utils/plugins&#x27;</span></span><br><span class="line"><span class="comment">// ~ 导入 自定义事件过滤器</span></span><br><span class="line"><span class="title class_">Vue</span>.<span class="title function_">use</span>(time)</span><br></pre></td></tr></table></figure>

<blockquote>
<p>在需要的Vue文件 使用过滤器 过滤器使用<code>|</code> <code>main.vue</code></p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&lt;span&gt;&#123;&#123;item.pubdate|formatTime&#125;&#125;&lt;/span&gt;</span><br></pre></td></tr></table></figure>



<h2 id="制作头像失效问题插件"><a href="#制作头像失效问题插件" class="headerlink" title="制作头像失效问题插件"></a>制作头像失效问题插件</h2><br>

<blockquote>
<p><code>目标</code>：处理图片加载失败时的默认显示效果 如果默认图片加载失败 用统一图片代替</p>
<p><code>原理 </code>:  这里用到了Vue的插件机制 <code>install</code> 和 自定义Vue方法 <code>Vue.directive</code></p>
</blockquote>
<ul>
<li>在入口文件中导入Vue插件 并且实例化Vue插件 <code>main.js</code></li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 导入自定义插件</span></span><br><span class="line"><span class="keyword">import</span> <span class="title class_">MyPlugins</span> <span class="keyword">from</span> <span class="string">&#x27;@/utils/plugins.js&#x27;</span></span><br><span class="line"><span class="comment">// 配置自定义插件</span></span><br><span class="line"><span class="comment">// 参数1 是需要实例化的插件名称</span></span><br><span class="line"><span class="comment">// 参数2 是定制插件内部的信息(插件接收的数据 传递给install的options参数 可设置为固定值)</span></span><br><span class="line"><span class="title class_">Vue</span>.<span class="title function_">use</span>(<span class="title class_">MyPlugins</span>, <span class="string">&#x27;&quot;https://tva2.sinaimg.cn/large/005INI3Xly8grusluz3ruj30b40b4wfn.jpg&quot;&#x27;</span>)</span><br></pre></td></tr></table></figure>

<ul>
<li>封装设置Vue插件 <code>utils文件夹 plugins.js</code><ul>
<li>使用到了自定义指令<code>Vue.directive</code>  来检测图片加载失败清空<ul>
<li>el:指令绑定的元素</li>
<li>bindings表示指令相关的配置信息 <font color = #ff3040>常用于动态绑定 <code>:</code></font></li>
</ul>
</li>
<li><code>onerror</code>是检测原始是否加载失败 如果失败就执行<ul>
<li>当图片有地址 但是地址没有加载成功的时候 会报错 会触<code>onerror</code></li>
</ul>
</li>
<li><font color = #ff3040>指令的名字在定义时候不需要加v-，在是用的时候才需要加v-</font></li>
</ul>
</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 定义一个Vue插件并导出</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">  <span class="comment">// Vue.use(MyPlugins, &#x27;defaultImg.png&#x27;)</span></span><br><span class="line">  <span class="comment">// Vue.use的参数二传递给install方法的第二个参数options</span></span><br><span class="line">  install (<span class="title class_">Vue</span>, options) &#123;</span><br><span class="line">    <span class="comment">// 扩展自定义指令</span></span><br><span class="line">    <span class="title class_">Vue</span>.<span class="title function_">directive</span>(<span class="string">&#x27;imgerror&#x27;</span>, &#123;</span><br><span class="line">      inserted (el, bindings) &#123;</span><br><span class="line">        <span class="comment">// el表示指令绑定的元素</span></span><br><span class="line">        <span class="comment">// bindings表示指令相关的配置信息</span></span><br><span class="line">        <span class="variable language_">console</span>.<span class="title function_">log</span>(bindings)</span><br><span class="line">        <span class="comment">// 图片加载成功</span></span><br><span class="line">        <span class="comment">// el.onload = function () &#123;&#125;</span></span><br><span class="line">        <span class="comment">// 图片加载失败</span></span><br><span class="line">        el.<span class="property">onerror</span> = <span class="keyword">function</span> (<span class="params"></span>) &#123;</span><br><span class="line">          <span class="comment">// 把加载失败的图片路径修改为指定的默认路径</span></span><br><span class="line">          <span class="comment">// 如果bindings.value有值就用它，否则用后面的options</span></span><br><span class="line">          el.<span class="property">src</span> = bindings.<span class="property">value</span> || options</span><br><span class="line">        &#125;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<br>

<ul>
<li>Vue文件 <code>template</code>模板 使用该插件 <ul>
<li>使用指令, 这里图片如果是用本地图片, 需要导入, 如果是完整地址的网图, 直接赋值即可</li>
<li>使用自定义Vue指令 需要 <code>v-自定义指令名称 </code>这样写法</li>
</ul>
</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">&lt;!-- :scr动态绑定img地址 --&gt;</span><br><span class="line">&lt;img v-imgerror=&quot;defaultImg&quot; :src=&quot;avatar&quot; class=&quot;user-avatar&quot;&gt;</span><br></pre></td></tr></table></figure>

<ul>
<li>Vue文件 <code>script</code>脚本 设置该插件<ul>
<li>设置图片的动态的地址 <code>data()</code>数据储存 中设置</li>
</ul>
</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 基于ES6导入单独的图片也是可以的(导入图片设置)</span></span><br><span class="line"><span class="keyword">import</span> <span class="title class_">Img</span> <span class="keyword">from</span> <span class="string">&#x27;@/assets/common/head.jpg&#x27;</span></span><br><span class="line"><span class="title function_">data</span>(<span class="params"></span>) &#123;</span><br><span class="line">  <span class="keyword">return</span> &#123;</span><br><span class="line">    <span class="attr">defaultImg</span>: <span class="title class_">Img</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;,</span><br><span class="line"><span class="comment">// 或者直接完整地址的网图赋值</span></span><br><span class="line"><span class="title function_">data</span>(<span class="params"></span>) &#123;</span><br><span class="line">  <span class="keyword">return</span> &#123;</span><br><span class="line">      <span class="comment">// 也可以用base64位 和 http图片</span></span><br><span class="line">    <span class="attr">defaultImg</span>: <span class="string">&#x27;https://dss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=2344451607,2404623174&amp;fm=111&amp;gp=0.jpg&#x27;</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;,</span><br></pre></td></tr></table></figure>

<p>总结：</p>
<ol>
<li>自定义指令的基本规则</li>
<li>插件基本使用规则：先定义，再导入并配置（支持选项）</li>
<li>配置插件时，可以传递options选项</li>
<li>扩展图片加载的自定义指令（原生dom事件 img.onerror 表示图片加载失败）</li>
<li>使用自定义指令</li>
</ol>

        </div>
        
            <div class="kratos-copyright text-center clearfix">
                <h5>本作品采用 <a rel="license nofollow" target="_blank" href="https://creativecommons.org/licenses/by-sa/4.0/">知识共享署名-相同方式共享 4.0 国际许可协议</a> 进行许可</h5>
            </div>
        
        <footer class="kratos-entry-footer clearfix">
            
                <div class="post-like-donate text-center clearfix" id="post-like-donate">
                
                
                    <a class="share" href="javascript:;"><i class="fa fa-share-alt"></i> 分享</a>
                    <div class="share-wrap" style="display: none;">
    <div class="share-group">
        <a href="javascript:;" class="share-plain qq" onclick="share('qq');" rel="nofollow">
            <div class="icon-wrap">
                <i class="fa fa-qq"></i>
            </div>
        </a>
        <a href="javascript:;" class="share-plain qzone" onclick="share('qzone');" rel="nofollow">
            <div class="icon-wrap">
                <i class="fa fa-star"></i>
            </div>
        </a>
        <a href="javascript:;" class="share-plain weixin pop style-plain" rel="nofollow">
            <div class="icon-wrap">
                <i class="fa fa-weixin"></i>
            </div>
            <div class="share-int">
                <div class="qrcode" id="wechat-qr"></div>
                <p>打开微信“扫一扫”，打开网页后点击屏幕右上角分享按钮</p>
            </div>
        </a>
        <a href="javascript:;" class="share-plain weibo" onclick="share('weibo');" rel="nofollow">
            <div class="icon-wrap">
                <i class="fa fa-weibo"></i>
            </div>
        </a>
        <a href="javascript:;" class="share-plain facebook style-plain" onclick="share('facebook');" rel="nofollow">
            <div class="icon-wrap">
                <i class="fa fa-facebook"></i>
            </div>
        </a>
        <a href="javascript:;" class="share-plain twitter style-plain" onclick="share('twitter');" rel="nofollow">
            <div class="icon-wrap">
                <i class="fa fa-twitter"></i>
            </div>
        </a>
    </div>
    <script type="text/javascript">
        $(()=>{
            new QRCode("wechat-qr", {
                text: "https://cuishikang.gitee.io/2021/06/30/Vue/3_Vue_install/",
                width: 150,
                height: 150,
                correctLevel : QRCode.CorrectLevel.H
            });
        });
        function share(dest) {
            const qqBase        = "https://connect.qq.com/widget/shareqq/index.html?";
            const weiboBase     = "https://service.weibo.com/share/share.php?";
            const qzoneBase     = "https://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?";
            const facebookBase  = "https://www.facebook.com/sharer/sharer.php?";
            const twitterBase   = "https://twitter.com/intent/tweet?";
            const hostUrl       = "https://cuishikang.gitee.io/2021/06/30/Vue/3_Vue_install/";
            const title         = "「Vue2 install插件案例」";
            const excerpt       = `::: tip 介绍Vue2插件 绝对时间插件 和 图片失效 制作方法 :::`;
            let _URL;
            switch (dest) {
                case "qq"       : _URL = qqBase+"url="+hostUrl+"&title="+title+"&desc=&summary="+excerpt+"&site=cxpy";     break;
                case "weibo"    : _URL = weiboBase+"url="+hostUrl+"&title="+title+excerpt;                                 break;
                case "qzone"    : _URL = qzoneBase+"url="+hostUrl+"&title="+title+"&desc=&summary="+excerpt+"&site=cxpy";  break;
                case "facebook" : _URL = facebookBase+"u="+hostUrl;                                                        break;
                case "twitter"  : _URL = twitterBase+"text="+title+excerpt+"&url="+hostUrl;                                break;
            }
            window.open(_URL);
        };
    </script>
</div>
                
                </div>
            
            <div class="footer-tag clearfix">
                <div class="pull-left">
                <i class="fa fa-tags"></i>
                    <a class="tag-none-link" href="/tags/Vue/" rel="tag">Vue</a>
                </div>
                <div class="pull-date">
                <span>最后编辑：2022-02-19</span>
                </div>
            </div>
        </footer>
    </div>
    
        <nav class="navigation post-navigation clearfix" role="navigation">
            
            <div class="nav-previous clearfix">
                <a title=" Vuex 的食用方法" href="/2021/06/29/Vue/2_Vuex/">&lt; 上一篇</a>
            </div>
            
            
            <div class="nav-next clearfix">
                <a title=" Vue中的echarts使用方法" href="/2021/07/08/Vue/3.1_Vue_echarts/">下一篇 &gt;</a>
            </div>
            
        </nav>
    
    
</article>

        

            </section>

        

                
            

<section id="kratos-widget-area" class="col-md-4 hidden-xs hidden-sm">
    <!-- 文章和页面根据splitter来分割，没有的话就从头开始设置为sticky -->
    
    
                <aside id="krw-about" class="widget widget-kratos-about clearfix">
    <div class="photo-background"></div>
    <div class="photo-wrapper clearfix">
        <div class="photo-wrapper-tip text-center">
            <img class="about-photo" src="https://gitee.com/cuishikang/tmp/raw/master/img/20220220194141.png" />
        </div>
    </div>
    <div class="textwidget">
        <p class="text-center"></p>
    </div>
    <div class="site-meta">
        <a class="meta-item" href="/archives/">
            <span class="title">
                文章
            </span>
            <span class="count">
                91
            </span>
        </a>
        <a class="meta-item" href="/categories/">
            <span class="title">
                分类
            </span>
            <span class="count">
                13
            </span>
        </a>
        <a class="meta-item" href="/tags/">
            <span class="title">
                标签
            </span>
            <span class="count">
                33
            </span>
        </a>
    </div>
</aside>
            
                    <div class="sticky-area">
                
                
  <aside id="krw-categories" class="widget widget-kratos-categories clearfix">
    <h4 class="widget-title"><i class="fa fa-folder"></i>分类目录</h4>
      <ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/categories/Css/">Css</a><span class="category-list-count">4</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/JavaScript/">JavaScript</a><span class="category-list-count">15</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/RABC/">RABC</a><span class="category-list-count">2</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/Vscode/">Vscode</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/Vue/">Vue</a><span class="category-list-count">13</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/Vue3/">Vue3</a><span class="category-list-count">25</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/Vue%E7%A7%BB%E5%8A%A8%E5%A4%B4%E6%9D%A1%E9%A1%B9%E7%9B%AE/">Vue移动头条项目</a><span class="category-list-count">10</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/animation/">animation</a><span class="category-list-count">4</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/axios/">axios</a><span class="category-list-count">7</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/other/">other</a><span class="category-list-count">2</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/uniapp/">uniapp</a><span class="category-list-count">3</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/vue-element-admin/">vue-element-admin</a><span class="category-list-count">2</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E5%B0%8F%E7%A8%8B%E5%BA%8F/">小程序</a><span class="category-list-count">2</span></li></ul>
  </aside>


            
                
  <aside id="krw-tags" class="widget widget-kratos-tags clearfix">
    <h4 class="widget-title"><i class="fa fa-tags"></i>标签聚合</h4>
      <div class="tag-clouds">
        <a href="/tags/Css/" style="font-size: 0.66em;">Css</a> <a href="/tags/H5Video/" style="font-size: 0.6em;">H5Video</a> <a href="/tags/JavaScript/" style="font-size: 0.74em;">JavaScript</a> <a href="/tags/My-project/" style="font-size: 0.6em;">My project</a> <a href="/tags/QQ/" style="font-size: 0.6em;">QQ</a> <a href="/tags/RABC/" style="font-size: 0.6em;">RABC</a> <a href="/tags/Vant/" style="font-size: 0.71em;">Vant</a> <a href="/tags/VeeValidate/" style="font-size: 0.6em;">VeeValidate</a> <a href="/tags/Vscode/" style="font-size: 0.6em;">Vscode</a> <a href="/tags/Vue/" style="font-size: 0.8em;">Vue</a> <a href="/tags/Vue-Router/" style="font-size: 0.6em;">Vue Router</a> <a href="/tags/Vue3/" style="font-size: 0.77em;">Vue3</a> <a href="/tags/Vuex/" style="font-size: 0.6em;">Vuex</a> <a href="/tags/Vue%E7%A7%BB%E5%8A%A8%E7%AB%AF/" style="font-size: 0.74em;">Vue移动端</a> <a href="/tags/Windows/" style="font-size: 0.6em;">Windows</a> <a href="/tags/animation/" style="font-size: 0.63em;">animation</a> <a href="/tags/axios/" style="font-size: 0.69em;">axios</a> <a href="/tags/echarts/" style="font-size: 0.6em;">echarts</a>
      </div>
  </aside>

            
                
  <aside id="krw-posts" class="widget widget-kratos-posts">
  <h4 class="widget-title"><i class="fa fa-file"></i>最新文章</h4>
  <div class="tab-content">
      <ul class="list-group">
        
        
          
          
            <a class="list-group-item" href="/2111/02/19/JavaScript/5_algorithm_tree/"><i class="fa  fa-book"></i> JavaScript树形数据转换算法</a>
            
          
        
          
          
            <a class="list-group-item" href="/2099/01/27/Vue3/1.1.1_Vue_study/"><i class="fa  fa-book"></i> Vue3 学习手册(博客版)</a>
            
          
        
          
          
            <a class="list-group-item" href="/2022/02/28/JavaScript/0.2_scroll/"><i class="fa  fa-book"></i> JS 滚动到指定位置和回到顶部</a>
            
          
        
          
          
            <a class="list-group-item" href="/2022/02/23/Vue/5_Vue_nextTick/"><i class="fa  fa-book"></i> 理解$nextTick</a>
            
          
        
          
          
            <a class="list-group-item" href="/2022/02/20/hello-world/"><i class="fa  fa-book"></i> Hello World</a>
            
          
        
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
      </ul>
  </div>
  </aside>

            
    </div>
</section>
        
        </div>
    </div>
</div>
<footer>
    <div id="footer"  class="ap-lrc"  >
        <div class="container">
            <div class="row">
                <div class="col-md-6 col-md-offset-3 footer-list text-center">
                    <ul class="kratos-social-icons">
                        
                        
                        <li><a target="_blank" rel="nofollow" href="https://t.me/CandyUnion"><i class="fa fa-telegram"></i></a></li>
                        
                        
                        
                        <li><a target="_blank" rel="me" href="https://nya.one/@Candinya"><i class="fa fa fa-share-alt-square"></i></a></li>
                        <li><a target="_blank" rel="nofollow" href="https://github.com/Candinya"><i class="fa fa-github"></i></a></li>
                        
                    </ul>
                    <ul class="kratos-copyright">
                        <div>
                            <li>&copy; 2022 小崔的博客 版权所有.</li>
                            <li>本站已运行<span id="span_dt">Loading...</span></li>
                        </div>
                        <div>
                            <li>Theme <a href="https://github.com/Candinya/Kratos-Rebirth" target="_blank">Kratos:Rebirth</a></li>
                            <li>Site built with&nbsp;<i class="fa fa-heart throb" style="color:#d43f57"></i>&nbsp;by cuishikang.</li>
                        </div>
                        <div>
                            <li>Powered by <a href="https://hexo.io" target="_blank" rel="nofollow">Hexo</a></li>
                            <li>Hosted on <a href="https://github.io" target="_blank">Github Pages</a></li>
                        </div>
                        <div>
                            
                            
                        </div>
                    </ul>
                </div>
            </div>
        </div>
        <div class="kr-tool text-center">
            <div class="tool">
                
                    <div class="box search-box">
                        <a href="/search/">
                            <span class="fa fa-search"></span>
                        </a>
                    </div>
                
                
                    <div class="box theme-box" id="darkmode-switch">
                        <span class="fa fa-adjust"></span>
                    </div>
                
                
            </div>
            <div class="box gotop-box">
                <span class="fa fa-chevron-up"></span>
            </div>
        </div>
    </div>
</footer>
</div>
</div>

        <script defer src="https://unpkg.com/bootstrap@3.3.4/dist/js/bootstrap.min.js"></script>
<script defer src="https://unpkg.com/nprogress@0.2.0/nprogress.js"></script>
<script>
    if (!window.kr) {
        window.kr = {};
    }
    window.kr.notMobile = (!(navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i)));
    window.kr.siteRoot = "/";
</script>


    <script async src="/js/candy.min.js"></script>



    <script defer src="https://unpkg.com/aplayer@1.10.1/dist/APlayer.min.js"></script>
    
    <script defer src="https://unpkg.com/meting@2/dist/Meting.min.js"></script>
    <meting-js
        server="netease"
        type="song"
        id="1375305989"
        order="random"
        fixed="true"
    >
    </meting-js>



    <script defer src="https://unpkg.com/@fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.js"></script>

<script defer src="https://unpkg.com/clipboard@2.0.6/dist/clipboard.min.js"></script>
<script defer src="/js/kratosr.min.js"></script>
<script defer src="/js/pjax.min.js"></script>


    <script defer src="/js/kr-dark.min.js"></script>



<!-- Extra support for third-party plguins  -->


    </body>
</html>